package com.valuedaas.schoolsafety.controller.resource;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.model.Filters;
import com.valuedaas.schoolsafety.common.base.Result;
import com.valuedaas.schoolsafety.common.collects.sys.SysDic;
import com.valuedaas.schoolsafety.controller.BaseController;
import com.valuedaas.schoolsafety.controller.ControllerHelper;
import com.valuedaas.schoolsafety.controller.resource.vo.DicVO;
import com.valuedaas.schoolsafety.exception.ParamValidateException;
import com.valuedaas.schoolsafety.service.recources.DicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 字典信息维护 控制器
 *
 * @author chenhua
 */
@RestController
@RequestMapping(value = "/dic/", produces = {
        "application/json;charset=utf-8"})
@Api(description = "基础管理-字典管理", value = "DicController")
public class DicController extends BaseController {

    @Autowired
    private DicService dicService;


    @ApiOperation(value = "添加一个字典信息", notes = "添加一个字典信息")
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public String add(@RequestBody @Valid DicVO dic, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error("验证失败", ControllerHelper.errorsInfo(result));
        }
        //验证重复
        if (dicService.checkExist(dic.getDicKey())) {
            throw new ParamValidateException("字典Key已存在");
        }
        Document d = Document.parse(JSON.toJSONString(dic));
        dicService.addDic(d);
        return Result.ok("数据保存成功", null);
    }


    @ApiOperation(value = "根据dicKey获取字典详情", notes = "根据dicKey获取字典详情")
    @RequestMapping(value = "detail.do", method = RequestMethod.GET)
    public String getBydicKey(@RequestParam(value = "dicKey") String dicKey) {
        Document doc = dicService.getBydicKey(dicKey);
        if (doc == null) {
            Result.error("不存在该字典", doc);
        }
        return Result.ok("数据获取成功", doc);
    }


    @ApiOperation(value = "删除一个字典信息", notes = "删除一个字典信息")
    @RequestMapping(value = "delete.do", method = RequestMethod.POST)
    public String delete(@RequestParam(value = "id", required = true) String id) {
        boolean b = dicService.deleteDic(id);
        if (b) {
            return Result.ok("数据删除成功", null);
        }
        return Result.error("数据删除失败", null);
    }

    @ApiOperation(value = "更新一个字典信息", notes = "更新一个字典信息")
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public String update(@RequestBody @Valid DicVO dic, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error("验证失败", ControllerHelper.errorsInfo(result));
        }
        if (StringUtils.isEmpty(dic.get_id())) {
            throw new ParamValidateException("id不能为空");
        }
        //验证重复
        if (dicService.checkExistForUpdate(dic.getDicKey(), dic.get_id())) {
            throw new ParamValidateException("字典Key已存在");
        }
        Document updateDoc = Document.parse(JSON.toJSONString(dic));
        boolean b = dicService.updateDic(dic.get_id(), updateDoc);
        if (b) {
            return Result.ok("数据修改成功", null);
        }
        return Result.error("数据修改失败", null);
    }

    @ApiOperation(value = "分页获取字典列表", notes = "获取字典列表")
    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public String list(
            @RequestParam(value = "dicKeys", required = false) List<String> dicKeys,
            @RequestParam(value = "dicName", required = false) String dicName,
            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        List<Bson> bsonList = new ArrayList<>();
        if (StringUtils.isNotEmpty(dicName)) {
            bsonList.add(Filters.eq(SysDic.DICNAME, Pattern.compile("^.*" + dicName + ".*$")));
        }
        if (CollectionUtils.isNotEmpty(dicKeys)) {
            bsonList.add(Filters.in(SysDic.DICKEY, dicKeys));
        }
        Map<String, Object> map = dicService.listDic(bsonList.size() > 0 ? Filters.and(bsonList) : new Document(), pageNo, pageSize);
        return Result.ok("列表", map);
    }
}
